package com.rainwood.water_authority.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rainwood.water_authority.R;
import com.rainwood.water_authority.base.MyBaseAdapter;
import com.rainwood.water_authority.model.ModuleBean;

/**
 * @author sxs
 * @date :2020/9/28
 * @desc: 模块适配器
 */
public final class ModuleBaseAdapter extends MyBaseAdapter<ModuleBean> {
    public ModuleBaseAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private class ViewHolder extends MyBaseAdapter.ViewHolder {

        private final ImageView mResView;
        private final TextView mBadgeNumView;
        private final TextView mModuleNameView;

        public ViewHolder() {
            super(R.layout.item_module);
            mResView = (ImageView) findViewById(R.id.iv_res);
            mBadgeNumView = (TextView) findViewById(R.id.tv_badge_num);
            mModuleNameView = (TextView) findViewById(R.id.tv_module_name);
        }

        @Override
        public void onBindView(int position) {
            mResView.setImageResource(getItem(position).getRes());
            mBadgeNumView.setText(String.valueOf(getItem(position).getBadge()));
            mModuleNameView.setText(getItem(position).getName());

            mBadgeNumView.setVisibility(getItem(position).getBadge() == 0 ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    protected RecyclerView.LayoutManager generateDefaultLayoutManager(Context context) {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 4);
        return layoutManager;
    }
}
